﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.Enum.User;

namespace Hi_Baidu.Packet.InPacket
{
    /// <summary>
    ///  * 登陆反馈包
    ///  * 基础包结构
    ///  * LOGINSTATE 登录状态(true/false) : bool
    ///  * MESSION 用户权限(登录失败时为 -1) : int
    ///  * USERNAME 用户名 : String
    /// </summary>
    class LoginReplyPacket : BaseInPackage
    {
        public LoginReplyPacket(InPackage inpackage)
            : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //读取用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //得到登陆结果
            byte[] LoginState = new byte[1];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, LoginState, 0, 1);
            Offset += 1;
            //得到权限
            byte[] Mession = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Mession, 0, 4);
            Offset += 4;
            byte[] SessionLength = new byte[4];
            //得到会话ID长度
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, SessionLength, 0, 4);
            Offset += 4;
            byte[] Session = new byte[BitConverter.ToInt32(SessionLength, 0)];
            //得到会话ID
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Session, 0, Session.Length);
            Offset += Session.Length;
            byte[] Login = new byte[4];
            //读取上线状态
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Login, 0,4);

            //开始打入集合
            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("LOGINSTATE", LoginState);
            this.DIC_CONTENT.Add("MESSION", Mession);
            this.DIC_CONTENT.Add("SESSION", Session);
            this.DIC_CONTENT.Add("LOGIN", Login);
        }

        public override byte[] EncryptBody()
        {
            throw new NotImplementedException();
        }

        public override byte[] DecryptBody()
        {
            throw new NotImplementedException();
        }

        public override int GetcryptograshStart()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     当前登录状态
        /// </summary>
        /// <returns>
        ///     返回true, 则表示登录成功
        /// </returns>
        public bool GetLoginState()
        {
            return BitConverter.ToBoolean(this.DIC_CONTENT["LOGINSTATE"], 0);
        }

        /// <summary>
        ///     当前在线状态
        /// </summary>
        /// <returns>
        ///     返回在线状态
        /// </returns>
        public ONLIN_STATE GetOnlineState()
        {
            return (ONLIN_STATE)BitConverter.ToInt32(this.DIC_CONTENT["LOGIN"], 0);
        }

        /// <summary>
        ///     登录状态
        /// </summary>
        public enum LoginState
        {
            /// <summary>
            ///     在线
            /// </summary>
            Normal,
            /// <summary>
            ///     隐身
            /// </summary>
            Hidden,
            /// <summary>
            ///     忙碌
            /// </summary>
            Busy,
            /// <summary>
            ///     离开
            /// </summary>
            Away
        }
    }
}
